<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="用户名称" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            prefix-icon="el-icon-user"
            class="search-input"
          />
        </el-form-item>
        <el-form-item label="推荐类型" prop="type">
          <el-select
            v-model="queryParams.type"
            placeholder="请选择推荐类型"
            clearable
            class="type-select"
          >
            <el-option label="在线推荐" value="OnLine">
              <i class="el-icon-video-play"></i>
              <span>在线推荐</span>
            </el-option>
            <el-option label="离线推荐" value="OffLine">
              <i class="el-icon-download"></i>
              <span>离线推荐</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="handleQuery"
            class="search-btn"
            >搜索</el-button
          >
          <el-button
            icon="el-icon-refresh"
            @click="resetQuery"
            class="reset-btn"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 推荐结果展示区域 -->
    <div class="recommendations-container">
      <el-row :gutter="20">
        <el-col
          :span="24"
          v-for="(userGroup, userName) in groupedRecommendations"
          :key="userName"
        >
          <el-card class="user-card" shadow="hover">
            <div slot="header" class="user-header">
              <div class="user-info">
                <el-avatar
                  icon="el-icon-user-solid"
                  :size="50"
                  class="user-avatar"
                ></el-avatar>
                <div class="user-details">
                  <h3 class="user-name">{{ userName }}</h3>
                  <div class="recommend-stats">
                    <span class="stat-item online">
                      <i class="el-icon-video-play"></i>
                      {{ userGroup.OnLine ? userGroup.OnLine.length : 0 }}
                      首在线推荐
                    </span>
                    <span class="stat-item offline">
                      <i class="el-icon-download"></i>
                      {{ userGroup.OffLine ? userGroup.OffLine.length : 0 }}
                      首离线推荐
                    </span>
                  </div>
                </div>
              </div>
            </div>

            <!-- 在线推荐模块 -->
            <div
              v-if="userGroup.OnLine && userGroup.OnLine.length > 0"
              class="recommendation-section online-section"
            >
              <div class="section-header">
                <div class="section-title">
                  <i class="el-icon-video-play"></i>
                  <span>在线推荐</span>
                  <span class="section-count"
                    >{{ userGroup.OnLine.length }} 首</span
                  >
                </div>
              </div>
              <el-row :gutter="15">
                <el-col
                  :span="4"
                  v-for="(item, index) in userGroup.OnLine"
                  :key="index"
                >
                  <el-card class="song-card" shadow="hover">
                    <div class="song-info">
                      <div class="song-icon">
                        <i class="el-icon-headset"></i>
                      </div>
                      <div class="song-details">
                        <div class="song-name">{{ item.songName }}</div>
                        <div class="recommend-time">
                          <i class="el-icon-time"></i>
                          {{ formatTime(item.recommendTime) }}
                        </div>
                      </div>
                    </div>
                    <div class="card-actions">
                      <el-button
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(item)"
                        v-hasPermi="['recommendations:recommendations:remove']"
                        class="delete-btn"
                        >删除</el-button
                      >
                    </div>
                  </el-card>
                </el-col>
              </el-row>
            </div>

            <!-- 离线推荐模块 -->
            <div
              v-if="userGroup.OffLine && userGroup.OffLine.length > 0"
              class="recommendation-section offline-section"
            >
              <div class="section-header">
                <div class="section-title">
                  <i class="el-icon-download"></i>
                  <span>离线推荐</span>
                  <span class="section-count"
                    >{{ userGroup.OffLine.length }} 首</span
                  >
                </div>
              </div>
              <el-row :gutter="15">
                <el-col
                  :span="4"
                  v-for="(item, index) in userGroup.OffLine"
                  :key="index"
                >
                  <el-card class="song-card" shadow="hover">
                    <div class="song-info">
                      <div class="song-icon">
                        <i class="el-icon-headset"></i>
                      </div>
                      <div class="song-details">
                        <div class="song-name">{{ item.songName }}</div>
                        <div class="recommend-time">
                          <i class="el-icon-time"></i>
                          {{ formatTime(item.recommendTime) }}
                        </div>
                      </div>
                    </div>
                    <div class="card-actions">
                      <el-button
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(item)"
                        v-hasPermi="['recommendations:recommendations:remove']"
                        class="delete-btn"
                        >删除</el-button
                      >
                    </div>
                  </el-card>
                </el-col>
              </el-row>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import {
  listRecommendations,
  delRecommendations,
} from "@/api/recommendations/recommendations";

export default {
  name: "Recommendations",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 推荐结果表格数据
      recommendationsList: [],
      // 查询参数
      queryParams: {
        userName: null,
        type: null,
      },
      // 分组后的数据
      groupedRecommendations: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询推荐结果列表 */
    getList() {
      this.loading = true;
      listRecommendations(this.queryParams).then((response) => {
        this.recommendationsList = response;
        this.groupRecommendations();
        this.loading = false;
      });
    },
    /** 分组处理推荐数据 */
    groupRecommendations() {
      const grouped = {};
      this.recommendationsList.forEach((item) => {
        if (!grouped[item.userName]) {
          grouped[item.userName] = {
            OnLine: [],
            OffLine: [],
          };
        }
        if (item.type === "OnLine") {
          grouped[item.userName].OnLine.push(item);
        } else if (item.type === "OffLine") {
          grouped[item.userName].OffLine.push(item);
        }
      });
      this.groupedRecommendations = grouped;
    },
    /** 格式化时间 */
    formatTime(time) {
      if (!time) return "";
      return new Date(time).toLocaleString();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        userName: null,
        type: null,
      };
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recId = row.recId;
      this.$modal
        .confirm("是否确认删除该推荐记录？")
        .then(() => {
          return delRecommendations(recId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped lang="scss">
.app-container {
  padding: 20px;
  background-color: #f5f7fa;
}

.search-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  background: linear-gradient(135deg, #ffffff 0%, #f5f7fa 100%);

  .search-input {
    width: 220px;
  }

  .type-select {
    width: 200px;
  }

  .search-btn {
    background: linear-gradient(45deg, #409eff, #36a3f7);
    border: none;
    padding: 8px 20px;
    &:hover {
      background: linear-gradient(45deg, #36a3f7, #409eff);
    }
  }

  .reset-btn {
    background: #f5f7fa;
    border: 1px solid #dcdfe6;
    padding: 8px 20px;
    &:hover {
      background: #ecf5ff;
      color: #409eff;
      border-color: #c6e2ff;
    }
  }
}

.recommendations-container {
  margin-top: 20px;
}

.user-card {
  margin-bottom: 20px;
  border-radius: 8px;
  transition: all 0.3s ease;
  background: linear-gradient(135deg, #ffffff 0%, #f5f7fa 100%);

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  }

  .user-header {
    padding: 15px 20px;
    border-bottom: 1px solid #ebeef5;

    .user-info {
      display: flex;
      align-items: center;
      gap: 15px;

      .user-avatar {
        background: linear-gradient(45deg, #409eff, #36a3f7);
        box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
      }

      .user-details {
        .user-name {
          margin: 0;
          font-size: 20px;
          font-weight: 600;
          color: #303133;
          text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
        }

        .recommend-stats {
          margin-top: 8px;
          display: flex;
          gap: 20px;

          .stat-item {
            display: flex;
            align-items: center;
            gap: 5px;
            font-size: 14px;
            padding: 4px 10px;
            border-radius: 15px;

            &.online {
              background: rgba(64, 158, 255, 0.1);
              color: #409eff;
            }

            &.offline {
              background: rgba(103, 194, 58, 0.1);
              color: #67c23a;
            }

            i {
              font-size: 16px;
            }
          }
        }
      }
    }
  }
}

.recommendation-section {
  margin: 20px 0;
  padding: 0 20px;

  &.online-section {
    .section-header {
      border-left: 4px solid #409eff;
    }
  }

  &.offline-section {
    .section-header {
      border-left: 4px solid #67c23a;
    }
  }

  .section-header {
    padding: 10px 15px;
    margin-bottom: 20px;
    background: #f5f7fa;
    border-radius: 4px;

    .section-title {
      display: flex;
      align-items: center;
      gap: 8px;
      font-size: 16px;
      font-weight: 600;
      color: #303133;

      i {
        font-size: 18px;
      }

      .section-count {
        margin-left: auto;
        font-size: 14px;
        color: #909399;
        background: rgba(0, 0, 0, 0.05);
        padding: 2px 8px;
        border-radius: 10px;
      }
    }
  }
}

.song-card {
  margin-bottom: 10px;
  border-radius: 6px;
  transition: all 0.3s ease;
  background: linear-gradient(135deg, #ffffff 0%, #f5f7fa 100%);

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  .song-info {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px;

    .song-icon {
      width: 24px;
      height: 24px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: linear-gradient(45deg, #409eff, #36a3f7);
      border-radius: 50%;
      box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);

      i {
        font-size: 12px;
        color: #ffffff;
      }
    }

    .song-details {
      flex: 1;
      min-width: 0;

      .song-name {
        font-size: 12px;
        font-weight: 600;
        color: #303133;
        margin-bottom: 1px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .recommend-time {
        display: flex;
        align-items: center;
        gap: 3px;
        font-size: 10px;
        color: #909399;

        i {
          font-size: 10px;
        }
      }
    }
  }

  .card-actions {
    padding: 4px 6px;
    border-top: 1px solid #ebeef5;
    text-align: right;

    .delete-btn {
      color: #f56c6c;
      font-size: 10px;
      padding: 0;
      &:hover {
        color: #f78989;
      }
    }
  }
}

// 响应式调整
@media screen and (max-width: 1200px) {
  .el-col {
    width: 20%;
  }
}

@media screen and (max-width: 992px) {
  .el-col {
    width: 25%;
  }
}

@media screen and (max-width: 768px) {
  .el-col {
    width: 33.33%;
  }

  .search-card {
    .search-input,
    .type-select {
      width: 100%;
      margin-bottom: 10px;
    }
  }
}
</style>
